package cn.anc.aonicardv.module.ui.recorder;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.contrarywind.listener.OnItemSelectedListener;
import com.contrarywind.view.WheelView;

import java.util.Calendar;
import java.util.Date;

import androidx.annotation.Nullable;
import butterknife.BindView;
import butterknife.OnClick;
import cn.anc.aonicardv.Constant;
import cn.anc.aonicardv.R;
import cn.anc.aonicardv.R2;
import cn.anc.aonicardv.module.ui.MainActivity;
import cn.anc.aonicardv.module.ui.base.BaseActivity;
import cn.anc.aonicardv.util.DateUtils;
import cn.anc.aonicardv.util.DialogUtils;
import cn.anc.aonicardv.util.ui.CarSettingGuiHandler;
import cn.anc.aonicardv.widget.WarningDialog;
import cn.anc.httpcontrolutil.CarControl;
import cn.anc.httpcontrolutil.CarControlSettings;

/**
 * Created by yangdai on 2017/6/21.
 */

public class TimeSettingActivity extends BaseActivity implements OnTimeSelectListener, View.OnClickListener, OnItemSelectedListener, CarControl.OnHeartBeatCallBack {

    @BindView(R2.id.tv_title)
    TextView titleTv;
    @BindView(R2.id.tv_auto)
    TextView autoTv;
    @BindView(R2.id.tv_manual)
    TextView manualTv;
    @BindView(R2.id.rl_auto)
    RelativeLayout autoRl;
    @BindView(R2.id.rl_manual)
    RelativeLayout manualRl;
    private CarControlSettings.SettingItem settingItem;
    private TimePickerView pvTime;
    private Date currentDate;
    private Dialog progressDialog;
    private WarningDialog warningDialog;
    private DialogUtils dialogUtils;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_time_setting);
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initVariable() {
        dialogUtils = new DialogUtils();
        progressDialog = dialogUtils.getCustomProgressDialog(this);
        warningDialog = dialogUtils.getWarningDialog(this);
        String key = getIntent().getStringExtra(Constant.IntentKeyParam.SETTING_KEY);
        settingItem = CarControlSettings.getSettingItem(key);
        Calendar startDate = Calendar.getInstance();
        startDate.set(2019, 0, 1);
        Calendar endDate = Calendar.getInstance();
        endDate.set(2050, 11, 31);
        pvTime = new TimePickerBuilder(this, this).setCancelColor(Color.BLACK).setSubmitColor(Color.BLACK).setType(new boolean[]{true, true, true, true, true, false}).setRangDate(startDate, endDate).setTitleSize(16)
                .build();
    }

    @Override
    public void setListener() {
        WheelView year = (WheelView) pvTime.findViewById(R.id.year);
        WheelView month = (WheelView) pvTime.findViewById(R.id.month);
        WheelView day = (WheelView) pvTime.findViewById(R.id.day);
        WheelView hour = (WheelView) pvTime.findViewById(R.id.hour);
        WheelView min = (WheelView) pvTime.findViewById(R.id.min);
        Button btnSubmit = (Button) pvTime.findViewById(R.id.btnSubmit);
        Button btnCancel = (Button) pvTime.findViewById(R.id.btnCancel);
        btnSubmit.setText(R.string.ok);
        btnCancel.setText(R.string.cancel);
        btnSubmit.setAllCaps(false);
        btnCancel.setAllCaps(false);
        autoRl.setOnClickListener(this);
        manualRl.setOnClickListener(this);
        warningDialog.okTv.setOnClickListener(this);
        btnSubmit.setOnClickListener(this);
        year.setOnItemSelectedListener(this);
        month.setOnItemSelectedListener(this);
        day.setOnItemSelectedListener(this);
        hour.setOnItemSelectedListener(this);
        min.setOnItemSelectedListener(this);
    }

    @Override
    public void initData() {
        titleTv.setText(getString(R.string.car_setting_name_time));
        if (settingItem.settingItemValue.contains(CarSettingGuiHandler.SETTING_ITEM_TIME_OPTION_AUTO)) {
            updateUi(View.GONE, View.VISIBLE);
        } else {
            updateUi(View.VISIBLE, View.GONE);
        }

    }

    @OnClick(R2.id.tv_back)
    public void back() {
        finish();
    }

    @Override
    public void onTimeSelect(Date date, View v) {
        currentDate = date;
        String dateString = DateUtils.getDate(date, true, true, true, false, DateUtils.FORMAT_ONE, true);
        pvTime.setTitleText(dateString);
    }

    private void setting(Date date, final String type) {
        progressDialog.show();
        settingItem.settingItemValue = type;
        String value = type + "," + DateUtils.getDate(date, true, true, true, true, DateUtils.FORMAT_ONE, true).replace(" ", "_");
        CarControl.doSetSettingItemValue(settingItem.settingItemKey, value, new CarControl.OnCarControlCallback() {
            @Override
            public void OnCommandCallback(int code, CarControl.CommandResponseInfo info) {
                if (code < 0) {
                    return;
                }
                if (progressDialog.isShowing())
                    progressDialog.dismiss();
                if (CarSettingGuiHandler.SETTING_ITEM_TIME_OPTION_MANUAL.equals(type)) {
                    pvTime.dismiss();
                    finish();
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.rl_auto) {
            updateUi(View.GONE, View.VISIBLE);
            setting(new Date(), CarSettingGuiHandler.SETTING_ITEM_TIME_OPTION_AUTO);
            finish();
        } else if (i == R.id.rl_manual) {
            updateUi(View.VISIBLE, View.GONE);
            pvTime.setDate(Calendar.getInstance());
            pvTime.show();
            onTimeSelect(new Date(), null);
        } else if (i == R.id.btnSubmit) {
            if (currentDate == null)
                currentDate = new Date();
            setting(currentDate, CarSettingGuiHandler.SETTING_ITEM_TIME_OPTION_MANUAL);
        } else if (i == R.id.tv_ok) {
            warningDialog.dismiss();
            startActivity(new Intent(this, MainActivity.class));

        }
    }
    @Override
    public void onResume() {
        super.onResume();
        CarControl.setOnHeartBeatCallBack(this);
        if (!CarControl.IsConnected.get()){
            if (null != warningDialog) {
                if (warningDialog.isShowing()) {
                    warningDialog.dismiss();
                }
                if (!warningDialog.isShowing() && !isDestroyed()) {
                    warningDialog.setHint(getString(R.string.tip_cardv_disconnect)).show();
                }

            }
        }

    }
    private void updateUi(int gone, int visible) {
        manualTv.setVisibility(gone);
        autoTv.setVisibility(visible);
    }

    @Override
    public void onItemSelected(int index) {
        pvTime.returnData();
    }

    @Override
    public void OnHeartBeatConnectOK() {
    }

    @Override
    public void OnHeartBeatConnectFail() {
    }

    @Override
    public void OnHeartBeatDisconnect() {
        if (null != warningDialog)
            warningDialog.setHint(getString(R.string.tip_cardv_disconnect)).show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (progressDialog.isShowing())
            progressDialog.dismiss();
    }

}
